import { 
  // 投诉单接口 
  complaintDetail,
  complaintList,
  // 整改单接口
  rectificationDetail,
  rectificationComplatint,
  rectificationStatusAll,
  rectificationList,
  rectificationUpdateStatus,
  // 维修订单相关接口
  repairSingleMsg,
  repairSingleList,
  rectionAddComplain,
  // 维修设备订单
  repairChickDetail,
  repairStatus,
  repairUpdate,
  repairRecord,

  getOrderProcess,
} from 'service/actions/deviceRepair'
import { message } from 'antd'
import router from 'umi/router'
export default {
  namespace: 'deviceRepair',

  state: {
    repList: [],          // 维修单列表
    repairDetail: null,         // 维修详情
    
    complaintDetailData: {}, // 投诉单详情页数据
    compList: [],   // 投诉单的列表信息

    reftDetailData: {},      // 整改单的详情页数据
    reftComplatintData: {},  // 整改单详情的投诉操作
    statusAll: {},           // 整改单所有状态
    reftList: {},            // 整改单的列表信息
    updateStatus: {},        // 修改整改单的状态
    
    singleMsg: {},           // 维修单（订单）信息
    sparePart: {},           // 发放备品备件
  

    repairDetail: {},         // 维修详情
    // stateOptions: [],         // 维修单状态
    repairUpdateData: {},    // 修改维修单状态
    repairRecordData: {},    // 设备维修管理单记录

    // ----
    list: [] ,               // 列表
    detail: {},              // 详情
    stateOptions: [],        // 状态
    stateOptions2: [],       // 整改状态
    process: [], // 流程节点
  },

  effects: {
   // 维修单列表
   *getSingleList ({ payload }, { put, call }){
    const data = yield call(repairSingleList, payload)
    if (!data) { return }
    if (data && data.mesg !== '处理成功') {
      message.error(data.mesg)
    }
    yield put({
      type: 'updateState',
      payload: { list: data.data, repList: data.data  }
    })
  },

   // 维修单详情
   *getChickDetail ({ payload }, { put, call }){
    const data = yield call(repairChickDetail, payload)
    if (!data) { return }
    yield put({
      type: 'updateState',
      payload: { repairDetail: data.data }
    })
    return data
  },





    // 投诉单接口 -------------------------------------------------------
    // 投诉单详情
    *getComplaintDetail ({ payload }, { put, call }){
      const data = yield call(complaintDetail, payload)
      if (!data) { return }
      if (data.code === '-2') {
        router.push('/deviceRepair')
      } else {
        yield put({
          type: 'updateState',
          payload: { complaintDetailData: data.data }
        })
      }
    },

    // 投诉单列表
    *getComplaintList ({ payload }, { put, call }){
      const data = yield call(complaintList, payload)
      if (!data) { return }
      if (data && data.mesg !== '处理成功') {
        message.error(data.mesg)
      }
      yield put({
        type: 'updateState',
        payload: { list: data.data, compList: data.data }
      })
    },

    // 整改单接口 -------------------------------------------------------
    // 整改单详情页数据
    *getRectificationDetail ({ payload }, { put, call }){
      const data = yield call(rectificationDetail, payload)
      if (!data) { return }
      if (data.code === '-2') {
        router.push('/deviceRepair')
      } else {
        yield put({
          type: 'updateState',
          payload: { reftDetailData: data.data }
        })
      }
      return data
    },

    // 整改单投诉
    *getReftComplatintData ({ payload }, { put, call }){
      const data = yield call(rectificationComplatint, payload)
      if (!data) { return }
      return data
    },

    // 整改单所有状态
    *getReftStatusAll ({ payload }, { put, call }){
      const data = yield call(rectificationStatusAll, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { stateOptions2: data.data }
      })
    },

    // 整改单列表
    *getReftList ({ payload }, { put, call }){
      const data = yield call(rectificationList, payload)
      if (!data) { return }
      if (data && data.mesg !== '处理成功') {
        message.error(data.mesg)
      }
      yield put({
        type: 'updateState',
        payload: {reftList: data.data}
      })
    },

    // 整改单的状态
    *getUpdateStatus ({ payload }, { put, call }){
      const data = yield call(rectificationUpdateStatus, payload)
      if (!data) { return }
      return data
    },

    // 维修订单相关接口 -------------------------------------------------------
    // 获取维修单（订单）信息
    *getRepairSingleMsg ({ payload }, { put, call }){
      yield put({type: 'clean'})
      const data = yield call(repairSingleMsg, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { singleMsg: data }
      })
    },

    // 投诉 和 整改
    *getRectionAddComplain ({ payload }, { put, call }){
      const data = yield call(rectionAddComplain, payload)
      if (!data) { return }
      return data
    },

    // 维修单-----------------
    // 维修单详情
    *getChickDetail ({ payload }, { put, call }){
      const data = yield call(repairChickDetail, payload)
      if (!data) { return }
      if (data.code === '-2') {
        router.push('/deviceRepair')
      } else {
        yield put({
          type: 'updateState',
          payload: { repairDetail: data.data }
        })
      }
      return data
    },

    // 维修单所有状态
    *getRepairStatus({ payload }, { put, call }){
      const data = yield call(repairStatus, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { stateOptions: data.data }
      })
    },

    // 修改维修单状态
    *getRepairUpdate ({ payload }, { put, call }){
      const data = yield call(repairUpdate, payload)
      if (!data) { return }
      return data
    },

    // 获取设备维修管理单记录
    *getRepairRecord ({ payload }, { put, call }){
      const data = yield call(repairRecord, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { repairRecordData: data }
      })
    },

    // 流程节点 
    *getProcess ({ payload }, { put, call }){
      const data = yield call(getOrderProcess, payload)
      if (!data) { return }
      yield put({
        type: 'updateState',
        payload: { process: data.data }
      })
    },
  },

  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload }
    },
    // 清空数据
    clean(state) {
      return { 
        ...state,
        complaintDetailData: {},
        reftDetailData: {},
        repairDetail: {},
        repList: [],
        reftList: [],
        compList: [],
        process: [],
      }
    },
  }
}